//
//  BoundHomeViewController.swift
//  EasePods
//
//  Created by 李然 on 2024/4/8.
//

import UIKit
import MoyoungLib
import CoreBluetooth

class BoundHomeViewController: ViewController {
    
    override func makeUI() {
        super.makeUI()
        
        
        self.navigation.item.leftBarButtonItem = UIBarButtonItem(customView: settingBtn)
        settingBtn.rx.tap
            .subscribe(onNext: { [weak self] _ in
                guard let `self` = self else { return }
                self.showSettingMenu()
            }).disposed(by: rx.disposeBag)
        
        view.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(self.navigation.bar.snp.bottom).offset(5)
            make.leading.equalTo(20)
        }
        
        view.addSubview(iconView)
        iconView.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(24)
            make.centerX.equalToSuperview()
        }
        
        addDeviceBtn.rx.tap
            .subscribe(onNext: { [weak self] _ in
                guard let `self` = self else { return }
                guard BluetoothManager.shared.bleState.value == .poweredOn else {
                    let view = OpenBleAlertView()
                    UIView.showSheet(with: view)
                    return
                }
                guard UserDefaults.standard.bool(forKey: BoundGuideType.powerOn.userDetaultKey) else {
                    UserDefaults.standard.setValue(true, forKey: BoundGuideType.powerOn.userDetaultKey)
                    let vc = BoundGuideViewController()
                    vc.guideType = .powerOn
                    vc.nextAction = { [weak self] in
                        let vc = BoundConnectViewController()
                        self?.navigationController?.pushViewController(vc, animated: true)
                    }
                    self.navigationController?.pushViewController(vc, animated: true)
                    return
                }
                let vc = BoundConnectViewController()
                self.navigationController?.pushViewController(vc, animated: true)
                
            }).disposed(by: rx.disposeBag)
        
        view.addSubview(addDeviceBtn)
        addDeviceBtn.snp.makeConstraints { make in
            make.bottom.equalTo(-kSafeBottomMargin - 75)
            make.centerX.equalToSuperview()
            make.size.equalTo(CGSize(width: kScreenWidth - 32, height: 57))
        }
        
//        view.addSubview(qrScanBtn)
//        qrScanBtn.snp.makeConstraints { make in
//            make.centerX.equalToSuperview()
//            make.bottom.equalTo(addDeviceBtn.snp.top)
//            make.height.equalTo(57)
//        }
        

    }
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.text = R.string.localizable.bound_not_found_device_tip()
        label.textColor = R.color.common_3_text()
        label.font = Fonts.systemFont(ofSize: 32, weight: .semibold)
        label.numberOfLines = 0
        return label
    }()
    
    lazy var settingBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setImage(R.image.ic_home_setting(), for: .normal)
        return btn
    }()

    lazy var addDeviceBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.layer.cornerRadius = 6
        btn.backgroundColor = R.color.common_22_button_n()
        btn.setTitle(R.string.localizable.bound_scan_device_title(), for: .normal)
        btn.titleLabel?.font = Fonts.systemFont(ofSize: 18,weight: .semibold)
        btn.setTitleColor(R.color.common_24_button_text(), for: .normal)
        return btn
    }()
    
    lazy var qrScanBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle(R.string.localizable.bound_qr_scan_add_device_title(), for: .normal)
        btn.titleLabel?.font = Fonts.systemFont(ofSize: 18,weight: .semibold)
        btn.setTitleColor(R.color.common_3_text(), for: .normal)
        btn.setImage(R.image.ic_scan_code(), for: .normal)
        btn.setImageTitleLayout(.imgLeft, spacing: 8)
        return btn
    }()
    
    lazy var iconView: UIImageView = {
        let view = UIImageView()
        view.image = R.image.img_bound_first()
        return view
    }()
    
    class OpenBleAlertView: View {
        override func makeUI() {
            super.makeUI()
            
            snp.makeConstraints { make in
                make.width.equalTo(kScreenWidth)
            }
            
            let iconView = ImageView(image: R.image.img_common_permission_bluetooth())
            addSubview(iconView)
            iconView.snp.makeConstraints { make in
                make.top.equalTo(101)
                make.centerX.equalToSuperview()
            }
            
            let label = Label()
            label.textColor = R.color.common_3_text()
            label.font = Fonts.systemFont(ofSize: 18)
            label.numberOfLines = 0
            label.textAlignment = .center
            label.text = R.string.localizable.bound_open_ble_title()
            addSubview(label)
            label.snp.makeConstraints { make in
                make.top.equalTo(iconView.snp.bottom).offset(20)
                make.centerX.equalToSuperview()
                make.width.equalTo(kScreenWidth - 32)
            }
            
            let tipLabel = Label()
            tipLabel.textColor = R.color.common_23_text_second()
            tipLabel.font = Fonts.systemFont(ofSize: 14)
            tipLabel.numberOfLines = 0
            tipLabel.textAlignment = .center
            tipLabel.text = R.string.localizable.bound_open_ble_tip()
            addSubview(tipLabel)
            tipLabel.snp.makeConstraints { make in
                make.centerX.equalToSuperview()
                make.top.equalTo(label.snp.bottom).offset(12)
                make.width.equalTo(label)
                make.bottom.equalTo(-66 - kSafeBottomMargin)
            }
        }
    }
}

